<?php
    /**
     * Tables accesses class
     * @author anton
     *
     */
	class SystemAccessController extends Controller
	{
		public $page_title = LANG_SYSTEM_ACCESS;
		public $layout = 'main';
		public $module = 'administration';
		public $helpers = array ('html', 'htmlx', 'toolbars', 'grids|edit');
		public $name = 'system_access';		
		public $menuItem = 'roles';
		public $use_logging = false;
		
		/**
		 * Edit access
		 * @param $role_id
		 */
		function Edit($role_id)
		{
			$this->data['role_id'] = $role_id;
			$this->data['accesses'] = $this->controller_model->ListItems(array("role_id"=>$role_id));
			$role_object = $this->GetModelObject('roles');
			$this->data['role'] = $role_object->Get($role_id);
			
			if (!empty($this->request->post))
			{
				$this->saveAccess($this->request->post,$role_id);
				$this->redirect('roles/edit/'.$role_id);
			}
		}
			
		/**
		 * Save access
		 * @param $data
		 * @param $role_id
		 */
		function SaveAccess($data,$role_id)
		{
			$list_list = '0';
			$add_list = '0';
			$edit_list = '0';
			$view_list = '0';
			$delete_list = '0';
			
			foreach ($data['access'] as $key => $value)
			{
				if ($value['access_list'] == 1) $list_list = $list_list.', '.$key;
				if ($value['add'] == 1)	$add_list = $add_list.', '.$key;
				if ($value['edit'] == 1) $edit_list = $edit_list.', '.$key;
				if ($value['view'] == 1) $view_list = $view_list.', '.$key;
				if ($value['delete'] == 1) $delete_list = $delete_list.', '.$key;
			}
			
			//updates accesses
			$this->controller_model->Update(array
			    (
			    	'role_id' => $role_id, 
			    	'list_list' => $list_list, 
			    	'add_list' => $add_list, 
			    	'edit_list' => $edit_list, 
			    	'view_list' => $view_list, 
			    	'delete_list' => $delete_list
			    ), 
				'sp_system_access_update_list'
		    );
		}
		
		/**
		 * View access
		 * @param $role_id
		 */
		function View($role_id)
		{
			$this->data['role_id'] = $role_id;
			$this->data['accesses'] = $this->controller_model->ListItems(array("role_id" => $role_id));
			$role_object = $this->GetModelObject('roles');
			$this->data['role'] = $role_object->Get($role_id);
		}
	}
?>